<!--
	This file is part of ELCube.
	ELCube is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	ELCube is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Affero General Public License for more details.
	You should have received a copy of the GNU Affero General Public License
	along with ELCube.  If not, see <https://www.gnu.org/licenses/>.
-->
<template>
    <span v-if="!editMode">
      <template v-if="value !== undefined && value !== null">
          {{value | nkNumber(inputOptions.format||'0.00')}}
           <span class="addon-after" v-if="inputOptions.addonAfter">{{inputOptions.addonAfter}}</span>
      </template>
      <template v-else-if="designMode">
          {{1001 | nkNumber(inputOptions.format||'0.00')}}
           <span class="addon-after" v-if="inputOptions.addonAfter">{{inputOptions.addonAfter}}</span>
      </template>
      <span v-else class="empty"></span>
    </span>
    <span v-else-if="options">
        <a-select size="small"
                  v-model="val"
                  mode="default"
                  :options="options"
                  @change="selectChange">
        </a-select>
        <span class="addon-after" v-if="inputOptions.addonAfter">{{inputOptions.addonAfter}}</span>
    </span>
    <span v-else>
        <a-input-number size="small"
                        v-model="val"
                        @blur="blur"
                        :max="inputOptions.max"
                        :min="inputOptions.min"
                        :precision="inputOptions.digits||0"
                        :step="inputOptions.step||1"
        ></a-input-number>
        <span class="addon-after" v-if="inputOptions.addonAfter">{{inputOptions.addonAfter}}</span>
    </span>
</template>

<script>
    export default {
        props:{
            value: {},
            editMode: Boolean,
            inputOptions: {
                type:Object,
                default(){
                    return {}
                }
            },
            designMode: Boolean
        },
        data(){
            return {
                old: undefined
            }
        },
        mounted(){
            this.old = this.val;
        },
        computed:{
            options(){
                if(this.inputOptions && this.inputOptions.options){
                    return this.inputOptions.options.map(i=>{
                        return {key:i,label:i}
                    });
                }
            },
            val:{
                get(){
                    return this.value;
                },
                set(value){
                    this.$emit('input',value);
                }
            }
        },
        methods:{
            blur(){
                if(this.old !== this.val){
                    this.old = this.val;
                    this.$emit('change',{});
                }
            },
            selectChange(){
                this.blur();
            }
        }
    }
</script>

<style scoped>
    .addon-after{
        color: #aaaaaa;
    }
</style>